#第二题  统计目录大小
import os

#定义函数

def CountDirctory(path=os.getcwd()):
    '''
    :param path: 需要读取的目录，默认值为当前工作目录
    :return:返回大小
    '''
    totalSize=0.0
    if not os.path.isdir(path):
        return totalSize

    list = os.listdir(path)
    for s in list:
        childPath = os.path.join(path,s)
        if os.path.isfile(childPath):
            totalSize+=os.path.getsize(childPath)
        else:
            totalSize+=CountDirctory(childPath)
    return totalSize

def ConvertBytes(bytes):  
    '''
    把字节进行转换
    '''
    if bytes < 1024:  #比特  
        bytes = str(round(bytes, 2)) + ' B' #字节  
    elif bytes >= 1024 and bytes < 1024 * 1024:  
        bytes = str(round(bytes / 1024, 2)) + ' KB' #千字节  
    elif bytes >= 1024 * 1024:  
        bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #兆字节  
    return bytes  
#help(CountDirctory)

#主要逻辑
path = input('请输入要统计的目录')
totalSize = CountDirctory(path)
print("目录大小为:{}".format(ConvertBytes(totalSize)))